package eteam.aps.service.impl;

import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.mapper.ParamFromWindowingMapper;
import eteam.aps.mapper.ParamInfoMapper;
import eteam.aps.model.entity.ParamInfo;
import eteam.aps.model.vo.ParamFromWindowingVo;
import eteam.aps.model.vo.ParamInfoDtlVo;
import eteam.aps.model.vo.ParamInfoVo;
import eteam.aps.service.ParamInfoService;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import eteam.aps.systeminterface.model.vo.QueryInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 参数信息业务层
 *
 * @author WMT
 */
@Service
public class ParamInfoServiceImpl implements ParamInfoService {

	@Autowired
	private ParamInfoMapper paramInfoMapper;

	@Autowired
	private ParamFromWindowingMapper paramFromWindowingMapper;

	@Autowired
	private LoginUserRedisService loginUserRedisService;

	@Transactional
	@Override
	public int insert(ParamInfo paramInfo) throws Exception{
		VerifyTokenDto user = loginUserRedisService.getUser();
		paramInfo.setfId(Helper.GetId());
		paramInfo.setfState(ParentEntity.FSTATE_DRAFT);
		paramInfo.setfCreateUserId(user.getFuserid());
		LocalDateTime now = LocalDateTime.now();
		paramInfo.setfCreatorTime(now);
		paramInfo.setfLastModifyTime(now);
		paramInfo.setfBookCode(user.getFgrouporgid());
		return paramInfoMapper.insert(paramInfo);
	}

	@Transactional
	@Override
	public int delete(String fId) {
		return paramInfoMapper.deleteByFId(fId);
	}

	@Transactional
	@Override
	public int update(ParamInfo paramInfo) throws Exception{
		VerifyTokenDto user = loginUserRedisService.getUser();
		paramInfo.setfLastModifyUserId(user.getFuserid());
		paramInfo.setfLastModifyTime(LocalDateTime.now());
		return paramInfoMapper.updateOne(paramInfo);
	}

	@Override
	public JsonPage<ParamInfoVo> selectAll(Map<String, Object> map, int page, int pageSize) {
		map.put("fState",ParentEntity.FSTATE_DELETED);
		int totalCount = paramInfoMapper.selectCountAll(map);
		List<ParamInfoVo> list = null;
		if (totalCount > 0) {
			map.put("offset", (page - 1) * pageSize);
			map.put("limit", pageSize);
			list = paramInfoMapper.selectAll(map);
		}
		return JsonPage.builder(page, pageSize, totalCount, list);
	}

	@Override
	public int selectCountByFDpFrom(int fParamFrom) {
		return paramInfoMapper.selectCountByFDpFrom(fParamFrom);
	}

	@Override
	public int selectCountByFDpType(int fDpType) {
		return paramInfoMapper.selectCountByFDpType(fDpType);
	}

	@Override
	public int selectCountByFDpName(String fDpName) {
		return paramInfoMapper.selectCountByFDpName(fDpName);
	}

	@Override
	public int selectCountByFDpCode(String fDpCode) {
		return paramInfoMapper.selectCountByFDpCode(fDpCode);
	}

	@Override
	public int selectFStateByFId(String fid) {
		return paramInfoMapper.selectFStateByFId(fid);
	}

	@Override
	public ParamInfoVo selectByfId(String fId) {
		return paramInfoMapper.selectByfId(fId);
	}

	@Override
	public List<ParamFromWindowingVo> selectPfromDdc() {
		return paramFromWindowingMapper.selectPfromDdc(ParentEntity.FSTATE_DELETED);
	}

	@Override
	public List<ParamFromWindowingVo> selectPfromSo() {
		return paramFromWindowingMapper.selectPfromSo(QueryInfoVo.STATE_ENABLE);
	}

	@Override
	public List<ParamInfoDtlVo> selectParamDtl() {
		return paramInfoMapper.selectParamDtl(ParentEntity.FSTATE_DELETED);
	}

}
